<?php
include "otamu_config.php";

define("_VIEWPATH_","views/otamu/");

if (!isset($_GET['act'])) $_GET['act'] = '';
if (!isset($show_range)) $show_range = 0;
if (!isset($the_date)) $the_date = Date('d-m-Y');

$visitor_attributes = array('ic_new','name','gender');
$attributes = array('ic_new','name','gender','syarikat','jumpa_siapa','tingkat','jenis_kenderaan','no_kenderaan','no_pass','urusan');

switch ($_GET['act']) {

	case "student_list" :
		$sqlalpha = "SELECT distinct substr(name,1,1) AS alpha from pelawat_profiles";
		$resalpha = pg_query($sqlalpha);
		while($datalpha = pg_fetch_object($resalpha)){
			$alphas[] = $datalpha->alpha;
		}
		
		if(!isset($_GET['huruf'])) $huruf = "A";
		if(isset($_GET['huruf'])) $huruf = $_GET['huruf'];

		$sql = "SELECT * FROM pelawat_profiles WHERE name ILIKE '$huruf%' ORDER BY name ASC";		
		
		$res = pg_query($sql);

		include _VIEWPATH_."qq_layout.php";

		break;

	case "list" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		include _VIEWPATH_."$act.php";
		break;

	case "still_in" :
		$sql = "SELECT * from lawatan INNER JOIN profiles ON ic_new=profile_ic_new WHERE time_out ISNULL";
		$res = pg_exec($sql);
		include _VIEWPATH_."otamu_layout.php";
		break;


	case "checkin_mykad" :
		for ($i=0;$i<count($attributes);$i++) {
			$a = $attributes[$i];
			$$a = '';
		}

		if (!isset($_GET['mykadid'])) {
			$mykadid= "";
		}
		else {
			$mykadid = $_GET['mykadid'];
		}
		//($_GET['mykadid']) ? $mykadid = $_GET['mykadid'] : $mykadid= "";

		include _VIEWPATH_."otamu_layout.php";
		break;


	case "checkin_mykad_exist" :
		if ( !isset($ic_new) || $ic_new=='') {
			exitError("Invalid No KP");
		}
		$sql = "SELECT * FROM profiles WHERE ic_new='$ic_new'";
		$res = pg_query($sql);
		$n = pg_num_rows($res);
		$dat = pg_fetch_array($res);

		for ($i=0;$i<count($visitor_attributes);$i++) {
			$a = $attributes[$i];
			$b = $$a = $dat[$a];
			//echo "$b <br>";
		}

		$syarikat = '';
		if ($n > 0) {
			$sql = "SELECT id_pelawat,syarikat FROM lawatan WHERE id_pelawat='$ic_new'";
			$res = pg_query($sql);
			$m = pg_num_rows($res);
			if ($m > 0) {
				$dat = pg_fetch_object($res);
				$syarikat = $dat->syarikat;
				//echo "sarikat: $syarikat <br>";
			}
		}
		if (!isset($tingkat)) $tingkat = '';
		if (!isset($no_kenderaan)) $no_kenderaan = '';
		if (!isset($no_pass)) $no_pass = '';
		if (!isset($urusan)) $urusan = '';

		include _VIEWPATH_."otamu_layout.php";
		break;


	case "checkin_manual" :
		for ($i=0;$i<count($attributes);$i++) {
			$a = $attributes[$i];
			$$a = '';
		}

		include _VIEWPATH_."qq_layout.php";
		break;

	case "new_but_visitor_already_exist" :
		if ( !isset($ic_new) || $ic_new=='') {
			exitError("Invalid No KP");
		}
		$sql = "SELECT * FROM profiles WHERE ic_new='$ic_new'";
		$res = pg_query($sql);
		$n = pg_num_rows($res);
		$dat = pg_fetch_array($res);

		for ($i=0;$i<count($visitor_attributes);$i++) {
			$a = $attributes[$i];
			$b = $$a = $dat[$a];
			//echo "$b <br>";
		}

		$syarikat = '';
		if ($n > 0) {
			$sql = "SELECT id_pelawat,syarikat FROM lawatan WHERE id_pelawat='$ic_new'";
			$res = pg_query($sql);
			$m = pg_num_rows($res);
			if ($m > 0) {
				$dat = pg_fetch_object($res);
				$syarikat = $dat->syarikat;
				//echo "sarikat: $syarikat <br>";
			}
		}
		if (!isset($tingkat)) $tingkat = '';
		if (!isset($no_kenderaan)) $no_kenderaan = '';
		if (!isset($no_pass)) $no_pass = '';
		if (!isset($urusan)) $urusan = '';

		//include _VIEWPATH_."$act.php";
		include _VIEWPATH_."otamu_layout.php";
		break;

	case "create" :
		//check blacklist
		$sql = "SELECT ic_new FROM blacklists WHERE ic_new='$ic_new'";
		$b = pg_num_rows(pg_query($sql));
		if ($b>0) {
			exitError("Pelawat ini ada di dalam senarai-hitam.");
		}

		//check appointment
		$sql = "SELECT pelawat_ic_new FROM appointments WHERE pelawat_ic_new='$ic_new'";
		$b = pg_num_rows(pg_query($sql));
		if ($b>0) {
			exitError("Pelawat ini ada temujanji pada hari ini.");
		}

		if ($jumpa_siapa == '') exitError("Sila Pilih pegawai ditemui");
		//echo "create";exit;
		$btn = "<br><input type=button onclick='history.back()' value='Back'>";
		if (''==$ic_new) { echo "<font color=red>Sila Masukkan No Kad Pengenalan.</font>$btn";exit; }

		//check if already exist ic_new
		$sql = "SELECT ic_new FROM profiles WHERE ic_new='$ic_new'";
		$n = pg_num_rows(pg_query($sql));

		if (!isset($hidden_date) || $hidden_date=='') {
			$time = date("Y-m-d H:i:s");
		}
		else {
			$time = "$hidden_date ". date("H:i:s");
		}
		//echo "time=".$time;exit;


		if ($n>0) {
			$insert = "INSERT INTO lawatan(
								id_pelawat,
								syarikat,
								jumpa_siapa,
								tingkat,
								jenis_kenderaan,
								no_kenderaan,
								no_pass,
								urusan,
								time_in ) VALUES
								(	'$ic_new',
									'$syarikat',
									'$jumpa_siapa',
									'$tingkat',
									'$jenis_kenderaan',
									'$no_kenderaan',
									'$no_pass',
									'$urusan',
									'$time'
								)";
			$insertres = pg_query($insert);
		}
		if (0==$n) {
			$insert = "INSERT INTO profiles(
								name,
								ic_new,
								gender	) VALUES
								(	'$name',
									'$ic_new',
									'$gender'
								)";
			$insertres = pg_query($insert);


			$insert = "INSERT INTO lawatan(
								id_pelawat,
								syarikat,
								jumpa_siapa,
								tingkat,
								jenis_kenderaan,
								no_kenderaan,
								no_pass,
								urusan,
								time_in ) VALUES
								(	'$ic_new',
									'$syarikat',
									'$jumpa_siapa',
									'$tingkat',
									'$jenis_kenderaan',
									'$no_kenderaan',
									'$no_pass',
									'$urusan',
									'$time'
								)";
			$insertres = pg_query($insert);
		}

		//include _VIEWPATH_."$act.php";
		include _VIEWPATH_."otamu_layout.php";

		//header ("Location: otamu.php");
		break;

	default :

		//include _VIEWPATH_."home.php";
		$act = "home";
		include _VIEWPATH_."otamu_layout.php";
		if ($USE_FIXED_MENU) echo "sssioiuer   "; 

}
?>
